import java.util.LinkedList;
import java.util.Queue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 123
 * Date: 2023-06-02
 * Time: 20:58
 */
public class MyStack {

    public Queue<Integer> queue1  ;
    public Queue<Integer> queue2 ;
    public MyStack() {
      this.queue1 = new LinkedList<>() ;
      this.queue2 = new LinkedList<>() ;
    }

    public void push(int x) {
       if(this.queue1.isEmpty() && this.queue2.isEmpty()){
           this.queue1.offer(x) ;
           return ;
       }
       if(this.queue1.isEmpty()){
           this.queue2.offer(x) ;
           return;
       }
       if(this.queue2.isEmpty()){
           this.queue1.offer(x) ;
           return;
       }
    }

    public int pop() {
      if(this.queue2.isEmpty() && this.queue1.isEmpty()){
          return -1 ;
      }

        int ret = 0;

        if(this.queue2.isEmpty()){
          int size = this.queue1.size() ;

          for (int i = 0; i < size - 1  ; i++) {
              ret = this.queue1.peek() ;
              this.queue1.poll() ;
              this.queue2.offer(ret) ;
          }
          ret = this.queue1.peek() ;
          this.queue1.poll() ;

      }else if(this.queue1.isEmpty()){

          int size = this.queue2.size() ;

          for (int i = 0; i < size -1 ; i++) {
              ret = this.queue2.peek() ;
              this.queue2.poll() ;
              this.queue1.offer(ret) ;
          }
          ret = this.queue2.peek() ;
          this.queue2.poll() ;

      }
    return ret ;
    }

    public int top() {
      if(this.queue2.isEmpty() && this.queue1.isEmpty()){
          return -1 ;
      }
        int ret = 0 ;
      if(this.queue1.isEmpty()){
          int size = this.queue2.size() ;
          for (int i = 0; i < size; i++) {
              ret = this.queue2.peek() ;
              this.queue2.poll() ;
              this.queue1.offer(ret) ;
          }
      }else if(this.queue2.isEmpty()){
          int size = this.queue1.size() ;
          for (int i = 0; i < size; i++) {
              ret = this.queue1.peek() ;
              this.queue1.poll() ;
              this.queue2.offer(ret) ;
          }
      }
      return ret ;

    }

    public boolean empty() {
       if(this.queue2.isEmpty() && this.queue1.isEmpty()){
           return true ;
       }
        return false ;
    }
}